package com.j.lemon.learn.clickhouse;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;

public class CKDistributeDdl {
    public static void main(String[] args) throws SQLException {
        String driverClassName = "ru.yandex.clickhouse.ClickHouseDriver";
        String userName = "audit4a";
        String password = "rryzggTR";
        String url = "jdbc:clickhouse://10.28.149.247:18123/default";


        String localTableSql = "alter table nodeTest15 on cluster 'ck_cluster' add column testC Int32";
        String distributeTableSql = "alter table nodeTest17_all on cluster 'ck_cluster' add column testC Int32";

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setTestWhileIdle(true);
        dataSource.setMaxActive(100);
        dataSource.setMaxWait(60000);
        dataSource.setTestOnBorrow(false);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        try {
//            jdbcTemplate.execute(localTableSql);
            jdbcTemplate.execute(distributeTableSql);
        } catch (Exception e) {
            String message = e.getMessage();
            if(!message.contains("UNKNOWN_TABLE")){
                throw new RuntimeException(e);
            }
        }


    }
}
